package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.huiztech.maystall.ui.HorizontalListView;

public class ClassifyActivity extends Activity {

	private HorizontalListView clothListView, shoeListView, boxListView,
			peishiListView, jiajuListView, yidongListView;
	private List<Map<String, Object>> clothList, shoeList, boxList, peishiList,
			jiajuList, yidongList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_classify);

		clothListView = (HorizontalListView) findViewById(R.id.cloth_list);
		shoeListView = (HorizontalListView) findViewById(R.id.shoe_list);
		boxListView = (HorizontalListView) findViewById(R.id.box_list);
		peishiListView = (HorizontalListView) findViewById(R.id.peishi_list);
		jiajuListView = (HorizontalListView) findViewById(R.id.jiaju_list);
		yidongListView = (HorizontalListView) findViewById(R.id.yidong_list);

		clothList = new ArrayList<Map<String, Object>>();
		shoeList = new ArrayList<Map<String, Object>>();
		boxList = new ArrayList<Map<String, Object>>();
		peishiList = new ArrayList<Map<String, Object>>();
		jiajuList = new ArrayList<Map<String, Object>>();
		yidongList = new ArrayList<Map<String, Object>>();

		getData();
		
		clothListView.setAdapter(new GalleryAdapter(this, clothList));
		shoeListView.setAdapter(new GalleryAdapter(this, shoeList));
		boxListView.setAdapter(new GalleryAdapter(this, boxList));
		peishiListView.setAdapter(new GalleryAdapter(this, peishiList));
		jiajuListView.setAdapter(new GalleryAdapter(this, jiajuList));
		yidongListView.setAdapter(new GalleryAdapter(this, yidongList));
	}

	private void getData() {
		for (int i = 0; i < 5; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("image", R.drawable.ic_launcher);
			map.put("classify", "classify" + i);
			clothList.add(map);
			shoeList.add(map);
			boxList.add(map);
			peishiList.add(map);
			jiajuList.add(map);
			yidongList.add(map);
		}
	}

	class ViewHolder {
		ImageView imageView;
		TextView textView;
	}

	public class GalleryAdapter extends BaseAdapter {
		private Context mContext;
		private List<Map<String, Object>> mList;
		private LayoutInflater mInflater;

		public GalleryAdapter(Context context, List<Map<String, Object>> list) {
			mContext = context;
			mList = list;
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return mList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.classify_gallery_item,
						null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.gallery_item_image);

				holder.textView = (TextView) convertView
						.findViewById(R.id.gallery_item_text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.imageView.setBackgroundResource(Integer.valueOf(mList
					.get(position).get("image").toString()));

			holder.textView.setText(mList.get(position).get("classify")
					.toString());

			return convertView;
		}
	}
}
